<?php 
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");
include("include/dbcommon.php");
include("classes/searchclause.php");
session_cache_limiter("none");

include("include/gsp_commercial_details_report_variables.php");

if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"))
{
	echo "<p>".mlang_message("NO_PERMISSIONS")."<a href=\"login.php\">".mlang_message("BACK_TO_LOGIN")."</a></p>";
	return;
}

// Modify query: remove blob fields from fieldlist.
// Blob fields on an export page are shown using imager.php (for example).
// They don't need to be selected from DB in export.php itself.
$gQuery->ReplaceFieldsWithDummies(GetBinaryFieldsIndices());

//	Before Process event
if($eventObj->exists("BeforeProcessExport"))
	$eventObj->BeforeProcessExport($conn);

$strWhereClause="";
$strHavingClause="";
$selected_recs=array();
$options = "1";

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
include('include/xtempl.php');
include('classes/runnerpage.php');
$xt = new Xtempl();

$id = postvalue("id") != "" ? postvalue("id") : 1;
//array of params for classes
$params = array("pageType" => PAGE_EXPORT, "id" =>$id, "tName"=>$strTableName);
$params["xt"] = &$xt;
if(!$eventObj->exists("ListGetRowCount") && !$eventObj->exists("ListQuery"))
	$params["needSearchClauseObj"] = false;
$pageObject = new RunnerPage($params);

if (@$_REQUEST["a"]!="")
{
	$options = "";
	$sWhere = "1=0";	

//	process selection
	$selected_recs=array();
	if (@$_REQUEST["mdelete"])
	{
		foreach(@$_REQUEST["mdelete"] as $ind)
		{
			$keys=array();
			$keys["id"]=refine($_REQUEST["mdelete1"][mdeleteIndex($ind)]);
			$selected_recs[]=$keys;
		}
	}
	elseif(@$_REQUEST["selection"])
	{
		foreach(@$_REQUEST["selection"] as $keyblock)
		{
			$arr=explode("&",refine($keyblock));
			if(count($arr)<1)
				continue;
			$keys=array();
			$keys["id"]=urldecode($arr[0]);
			$selected_recs[]=$keys;
		}
	}

	foreach($selected_recs as $keys)
	{
		$sWhere = $sWhere . " or ";
		$sWhere.=KeyWhere($keys);
	}


	$strSQL = gSQLWhere($sWhere);
	$strWhereClause=$sWhere;
	
	$_SESSION[$strTableName."_SelectedSQL"] = $strSQL;
	$_SESSION[$strTableName."_SelectedWhere"] = $sWhere;
	$_SESSION[$strTableName."_SelectedRecords"] = $selected_recs;
}

if ($_SESSION[$strTableName."_SelectedSQL"]!="" && @$_REQUEST["records"]=="") 
{
	$strSQL = $_SESSION[$strTableName."_SelectedSQL"];
	$strWhereClause=@$_SESSION[$strTableName."_SelectedWhere"];
	$selected_recs = $_SESSION[$strTableName."_SelectedRecords"];
}
else
{
	$strWhereClause=@$_SESSION[$strTableName."_where"];
	$strHavingClause=@$_SESSION[$strTableName."_having"];
	$strSQL=gSQLWhere($strWhereClause,$strHavingClause);
}

$mypage=1;
if(@$_REQUEST["type"])
{
//	order by
	$strOrderBy=$_SESSION[$strTableName."_order"];
	if(!$strOrderBy)
		$strOrderBy=$gstrOrderBy;
	$strSQL.=" ".trim($strOrderBy);

	$strSQLbak = $strSQL;
	if($eventObj->exists("BeforeQueryExport"))
		$eventObj->BeforeQueryExport($strSQL,$strWhereClause,$strOrderBy);
//	Rebuild SQL if needed
	if($strSQL!=$strSQLbak)
	{
//	changed $strSQL - old style	
		$numrows=GetRowCount($strSQL);
	}
	else
	{
		$strSQL = gSQLWhere($strWhereClause,$strHavingClause);
		$strSQL.=" ".trim($strOrderBy);
		$rowcount=false;
		if($eventObj->exists("ListGetRowCount"))
		{
			$masterKeysReq=array();
			for($i = 0; $i < count($pageObject->detailKeysByM); $i ++)
				$masterKeysReq[]=$_SESSION[$strTableName."_masterkey".($i + 1)];
			$rowcount=$eventObj->ListGetRowCount($pageObject->searchClauseObj,$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs);
		}
		if($rowcount!==false)
			$numrows=$rowcount;
		else
			$numrows=gSQLRowCount($strWhereClause,$strHavingClause);
	}
	LogInfo($strSQL);

//	 Pagination:

	$nPageSize = 0;
	if(@$_REQUEST["records"]=="page" && $numrows)
	{
		$mypage = (integer)@$_SESSION[$strTableName."_pagenumber"];
		$nPageSize = (integer)@$_SESSION[$strTableName."_pagesize"];
		
		if(!$nPageSize)
			$nPageSize = GetTableData($strTableName,".pageSize",0);
				
		if($nPageSize<0)
			$nPageSize = 0;
			
		if($nPageSize>0)
		{
			if($numrows<=($mypage-1)*$nPageSize)
				$mypage = ceil($numrows/$nPageSize);
		
			if(!$mypage)
				$mypage = 1;
			
					$strSQL.=" limit ".(($mypage-1)*$nPageSize).",".$nPageSize;
		}
	}
	$listarray = false;
	if($eventObj->exists("ListQuery"))
		$listarray = $eventObj->ListQuery($pageObject->searchClauseObj,$_SESSION[$strTableName."_arrFieldForSort"],$_SESSION[$strTableName."_arrHowFieldSort"],$_SESSION[$strTableName."_mastertable"],$masterKeysReq,$selected_recs,$nPageSize,$mypage);
	if($listarray!==false)
		$rs = $listarray;
	elseif($nPageSize>0)
	{
					$rs = db_query($strSQL,$conn);
	}
	else
		$rs = db_query($strSQL,$conn);

	if(!ini_get("safe_mode"))
		set_time_limit(300);
	
	if(@$_REQUEST["type"]=="excel")
	{
//	remove grouping
		$locale_info["LOCALE_SGROUPING"]="0";
		$locale_info["LOCALE_SMONGROUPING"]="0";
		ExportToExcel();
	}
	else if(@$_REQUEST["type"]=="word")
	{
		ExportToWord();
	}
	else if(@$_REQUEST["type"]=="xml")
	{
		ExportToXML();
	}
	else if(@$_REQUEST["type"]=="csv")
	{
		$locale_info["LOCALE_SGROUPING"]="0";
		$locale_info["LOCALE_SDECIMAL"]=".";
		$locale_info["LOCALE_SMONGROUPING"]="0";
		$locale_info["LOCALE_SMONDECIMALSEP"]=".";
		ExportToCSV();
	}
	db_close($conn);
	return;
}

// add button events if exist
$pageObject->addButtonHandlers();

// add onload event
$onLoadJsCode = GetTableData($pageObject->tName, ".jsOnloadExport", '');
$pageObject->addOnLoadJsEvent($onLoadJsCode);

if($options)
{
	$xt->assign("rangeheader_block",true);
	$xt->assign("range_block",true);
}

$xt->assign("exportlink_attrs", 'id="saveButton'.$pageObject->id.'"');

$pageObject->body["begin"] .="<script type=\"text/javascript\" src=\"include/jquery.js\"></script>\r\n";
$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
if ($pageObject->debugJSMode === true)
{
	/*$pageObject->body['begin'] .= "<script type=\"text/javascript\" src=\"include/runnerJS/Runner.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Util.js\"></script>\r\n".
		"<script type=\"text/javascript\" src=\"include/runnerJS/Observer.js\"></script>\r\n";*/
	$pageObject->body['begin'] .= "<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";
}
else
	$pageObject->body["begin"].="<script language=\"JavaScript\" src=\"include/runnerJS/RunnerBase.js\"></script>\r\n";

$pageObject->fillSetCntrlMaps();
$pageObject->body['end'] .= '<script>';
$pageObject->body['end'] .= "window.controlsMap = '".jsreplace(my_json_encode($pageObject->controlsHTMLMap))."';";
$pageObject->body['end'] .= "window.settings = '".jsreplace(my_json_encode($pageObject->jsSettings))."';";
$pageObject->body['end'] .= '</script>';
$pageObject->addCommonJs();		
	
$pageObject->body["end"] .= "<script>".$pageObject->PrepareJS()."</script>";
$xt->assignbyref("body",$pageObject->body);

$xt->display("gsp_commercial_details_report_export.htm");

function ExportToExcel()
{
	global $cCharset;
	header("Content-Type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=gsp_commercial_details_report.xls");

	echo "<html>";
	echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
	
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToWord()
{
	global $cCharset;
	header("Content-Type: application/vnd.ms-word");
	header("Content-Disposition: attachment;Filename=gsp_commercial_details_report.doc");

	echo "<html>";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToXML()
{
	global $nPageSize,$rs,$strTableName,$conn,$eventObj;
	header("Content-Type: text/xml");
	header("Content-Disposition: attachment;Filename=gsp_commercial_details_report.xml");
	if($eventObj->exists("ListFetchArray"))
		$row = $eventObj->ListFetchArray($rs);
	else
		$row = db_fetch_array($rs);	
	if(!$row)
		return;
		
	global $cCharset;
	
	echo "<?xml version=\"1.0\" encoding=\"".$cCharset."\" standalone=\"yes\"?>\r\n";
	echo "<table>\r\n";
	$i=0;
	
	
	while((!$nPageSize || $i<$nPageSize) && $row)
	{
		$values = array();
			$values["id"] = GetData($row,"id","");
			$values["bcontact_id"] = GetData($row,"bcontact_id","");
			$values["account_id"] = GetData($row,"account_id","");
			$values["ship_mode_id"] = GetData($row,"ship_mode_id","");
			$values["ship_from_city"] = GetData($row,"ship_from_city","");
			$values["ship_from_country"] = GetData($row,"ship_from_country","");
			$values["ship_to_country"] = GetData($row,"ship_to_country","");
			$values["ship_to_port"] = GetData($row,"ship_to_port","");
			$values["ship_date"] = GetData($row,"ship_date","");
			$values["remarks"] = GetData($row,"remarks","");
			$values["support_documents"] = GetData($row,"support_documents","");
			$values["origin_requirements"] = GetData($row,"origin_requirements","");
			$values["book_nos"] = GetData($row,"book_nos","");
			$values["ship_status"] = GetData($row,"ship_status","");
			$values["status"] = GetData($row,"status","");
			$values["signatory"] = GetData($row,"signatory","");
			$values["pre_carriage_mode"] = GetData($row,"pre_carriage_mode","");
			$values["pre_carriage_from"] = GetData($row,"pre_carriage_from","");
			$values["pre_carriage_to"] = GetData($row,"pre_carriage_to","");
			$values["ref_id"] = GetData($row,"ref_id","");
		
		
		$eventRes = true;
		if ($eventObj->exists('BeforeOut'))
		{			
			$eventRes = $eventObj->BeforeOut($row, $values);
		}
		if ($eventRes)
		{
			$i++;
			echo "<row>\r\n";
			foreach ($values as $fName => $val)
			{
				$field = htmlspecialchars(XMLNameEncode($fName));
				echo "<".$field.">";
				echo htmlspecialchars($values[$fName]);
				echo "</".$field.">\r\n";
			}
			echo "</row>\r\n";
		}
		
		
		if($eventObj->exists("ListFetchArray"))
			$row = $eventObj->ListFetchArray($rs);
		else
			$row = db_fetch_array($rs);	
	}
	echo "</table>\r\n";
}

function ExportToCSV()
{
	global $rs,$nPageSize,$strTableName,$conn,$eventObj;
	header("Content-Type: application/csv");
	header("Content-Disposition: attachment;Filename=gsp_commercial_details_report.csv");
	
	if($eventObj->exists("ListFetchArray"))
		$row = $eventObj->ListFetchArray($rs);
	else
		$row = db_fetch_array($rs);	
	if(!$row)
		return;
	
		
		
	$totals=array();

	
// write header
	$outstr="";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"id\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"bcontact_id\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"account_id\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_mode_id\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_from_city\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_from_country\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_to_country\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_to_port\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_date\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"remarks\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"support_documents\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"origin_requirements\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"book_nos\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ship_status\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"status\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"signatory\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"pre_carriage_mode\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"pre_carriage_from\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"pre_carriage_to\"";
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"ref_id\"";
	echo $outstr;
	echo "\r\n";

// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
	{
		
		
		$values = array();
			$format="";
			$values["id"] = GetData($row,"id",$format);
			$format="";
			$values["bcontact_id"] = GetData($row,"bcontact_id",$format);
			$format="";
			$values["account_id"] = GetData($row,"account_id",$format);
			$format="";
			$values["ship_mode_id"] = GetData($row,"ship_mode_id",$format);
			$format="";
			$values["ship_from_city"] = GetData($row,"ship_from_city",$format);
			$format="";
			$values["ship_from_country"] = GetData($row,"ship_from_country",$format);
			$format="";
			$values["ship_to_country"] = GetData($row,"ship_to_country",$format);
			$format="";
			$values["ship_to_port"] = GetData($row,"ship_to_port",$format);
			$format="Short Date";
			$values["ship_date"] = GetData($row,"ship_date",$format);
			$format="";
			$values["remarks"] = GetData($row,"remarks",$format);
			$format="";
			$values["support_documents"] = GetData($row,"support_documents",$format);
			$format="";
			$values["origin_requirements"] = GetData($row,"origin_requirements",$format);
			$format="";
			$values["book_nos"] = GetData($row,"book_nos",$format);
			$format="";
			$values["ship_status"] = GetData($row,"ship_status",$format);
			$format="";
			$values["status"] = GetData($row,"status",$format);
			$format="";
			$values["signatory"] = GetData($row,"signatory",$format);
			$format="";
			$values["pre_carriage_mode"] = GetData($row,"pre_carriage_mode",$format);
			$format="";
			$values["pre_carriage_from"] = GetData($row,"pre_carriage_from",$format);
			$format="";
			$values["pre_carriage_to"] = GetData($row,"pre_carriage_to",$format);
			$format="";
			$values["ref_id"] = GetData($row,"ref_id",$format);

		$eventRes = true;
		if ($eventObj->exists('BeforeOut'))
		{			
			$eventRes = $eventObj->BeforeOut($row,$values);
		}
		if ($eventRes)
		{
			$outstr="";			
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["id"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["bcontact_id"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["account_id"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_mode_id"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_from_city"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_from_country"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_to_country"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_to_port"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_date"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["remarks"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["support_documents"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["origin_requirements"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["book_nos"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ship_status"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["status"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["signatory"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["pre_carriage_mode"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["pre_carriage_from"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["pre_carriage_to"]).'"';
			if($outstr!="")
				$outstr.=",";
			$outstr.='"'.str_replace('"','""',$values["ref_id"]).'"';
			echo $outstr;
		}
		
		$iNumberOfRows++;
		if($eventObj->exists("ListFetchArray"))
			$row = $eventObj->ListFetchArray($rs);
		else
			$row = db_fetch_array($rs);	
			
		if(((!$nPageSize || $iNumberOfRows<$nPageSize) && $row) && $eventRes)
			echo "\r\n";
	}
}


function WriteTableData()
{
	global $rs,$nPageSize,$strTableName,$conn,$eventObj;
	
	if($eventObj->exists("ListFetchArray"))
		$row = $eventObj->ListFetchArray($rs);
	else
		$row = db_fetch_array($rs);	
	if(!$row)
		return;
// write header
	echo "<tr>";
	if($_REQUEST["type"]=="excel")
	{
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","id")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","bcontact_id")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","account_id")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_mode_id")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_from_city")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_from_country")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_to_country")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_to_port")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_date")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","remarks")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","support_documents")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","origin_requirements")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","book_nos")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ship_status")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","status")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","signatory")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","pre_carriage_mode")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","pre_carriage_from")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","pre_carriage_to")).'</td>';	
		echo '<td style="width: 100" x:str>'.PrepareForExcel(GetFieldLabel("gsp_commercial_details_report","ref_id")).'</td>';	
	}
	else
	{
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","id")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","bcontact_id")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","account_id")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_mode_id")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_from_city")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_from_country")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_to_country")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_to_port")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_date")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","remarks")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","support_documents")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","origin_requirements")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","book_nos")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ship_status")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","status")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","signatory")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","pre_carriage_mode")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","pre_carriage_from")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","pre_carriage_to")."</td>";
		echo "<td>".GetFieldLabel("gsp_commercial_details_report","ref_id")."</td>";
	}
	echo "</tr>";

	$totals=array();
// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
	{
			
		$values = array();	

					
							$format="";
			
			$values["id"] = GetData($row,"id",$format);
					
							$format="";
			
			$values["bcontact_id"] = GetData($row,"bcontact_id",$format);
					
							$format="";
			
			$values["account_id"] = GetData($row,"account_id",$format);
					
							$format="";
			
			$values["ship_mode_id"] = GetData($row,"ship_mode_id",$format);
					
							$format="";
			
			$values["ship_from_city"] = GetData($row,"ship_from_city",$format);
					
							$format="";
			
			$values["ship_from_country"] = GetData($row,"ship_from_country",$format);
					
							$format="";
			
			$values["ship_to_country"] = GetData($row,"ship_to_country",$format);
					
							$format="";
			
			$values["ship_to_port"] = GetData($row,"ship_to_port",$format);
					
							$format="Short Date";
			
			$values["ship_date"] = GetData($row,"ship_date",$format);
					
							$format="";
			
			$values["remarks"] = GetData($row,"remarks",$format);
					
							$format="";
			
			$values["support_documents"] = GetData($row,"support_documents",$format);
					
							$format="";
			
			$values["origin_requirements"] = GetData($row,"origin_requirements",$format);
					
							$format="";
			
			$values["book_nos"] = GetData($row,"book_nos",$format);
					
							$format="";
			
			$values["ship_status"] = GetData($row,"ship_status",$format);
					
							$format="";
			
			$values["status"] = GetData($row,"status",$format);
					
							$format="";
			
			$values["signatory"] = GetData($row,"signatory",$format);
					
							$format="";
			
			$values["pre_carriage_mode"] = GetData($row,"pre_carriage_mode",$format);
					
							$format="";
			
			$values["pre_carriage_from"] = GetData($row,"pre_carriage_from",$format);
					
							$format="";
			
			$values["pre_carriage_to"] = GetData($row,"pre_carriage_to",$format);
					
							$format="";
			
			$values["ref_id"] = GetData($row,"ref_id",$format);

		
		$eventRes = true;
		if ($eventObj->exists('BeforeOut'))
		{			
			$eventRes = $eventObj->BeforeOut($row, $values);
		}
		if ($eventRes)
		{
			$iNumberOfRows++;
			echo "<tr>";

							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["id"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["bcontact_id"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["account_id"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ship_mode_id"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ship_from_city"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ship_from_country"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ship_to_country"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ship_to_port"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="Short Date";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["ship_date"]);
					else
						echo htmlspecialchars($values["ship_date"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["remarks"]);
					else
						echo htmlspecialchars($values["remarks"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["support_documents"]);
					else
						echo htmlspecialchars($values["support_documents"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["origin_requirements"]);
					else
						echo htmlspecialchars($values["origin_requirements"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["book_nos"]);
					else
						echo htmlspecialchars($values["book_nos"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["ship_status"]);
					else
						echo htmlspecialchars($values["ship_status"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["status"]);
					else
						echo htmlspecialchars($values["status"]);
			echo '</td>';
							if($_REQUEST["type"]=="excel")
					echo '<td x:str>';
				else
					echo '<td>';
						
			
									$format="";
									if($_REQUEST["type"]=="excel")
						echo PrepareForExcel($values["signatory"]);
					else
						echo htmlspecialchars($values["signatory"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["pre_carriage_mode"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["pre_carriage_from"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["pre_carriage_to"]);
			echo '</td>';
							echo '<td>';
						
			
									$format="";
									echo htmlspecialchars($values["ref_id"]);
			echo '</td>';
			echo "</tr>";
		}		
		
		
		if($eventObj->exists("ListFetchArray"))
			$row = $eventObj->ListFetchArray($rs);
		else
			$row = db_fetch_array($rs);	
	}
	
}

function XMLNameEncode($strValue)
{	
	$search=array(" ","#","'","/","\\","(",")",",","[");
	$ret=str_replace($search,"",$strValue);
	$search=array("]","+","\"","-","_","|","}","{","=");
	$ret=str_replace($search,"",$ret);
	return $ret;
}

function PrepareForExcel($str)
{
	$ret = htmlspecialchars($str);
	if (substr($ret,0,1)== "=") 
		$ret = "&#61;".substr($ret,1);
	return $ret;

}


?>
